package com.traffic.fuwu.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.traffic.fuwu.dao.DeviceMapper;
import com.traffic.fuwu.mybatis.pojo.Device;
import com.traffic.fuwu.mybatis.pojo.DeviceExample;
import com.traffic.fuwu.service.DeviceService;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<Map> selectDeviceFy(String lineName, String vehNo, String deviceId, String appKey, int startRow,
                                    int pageSize) {
        List<Map> sd = new ArrayList<>();
        try {
            sd = deviceMapper.selectDeviceFy(lineName, vehNo, deviceId, appKey, startRow, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sd;
    }

    @Override
    public void updateDevice(Device device) {
        this.deviceMapper.updateByPrimaryKey(device);
    }

    @Override
    public void addDevice(Device device) {
        deviceMapper.insert(device);
    }

    @Override
    public int countDevice(String lineName, String vehNo, String deviceId, String appKey) {
        return deviceMapper.countDevice(lineName, vehNo, deviceId, appKey);
    }

    @Override
    public Device getDevice(String deviceId) {
        DeviceExample example = new DeviceExample();
        example.or().andDeviceIdEqualTo(deviceId);
        List<Device> list = deviceMapper.selectByExample(example);
        if (list != null && list.size() != 0) {
            Device device = list.get(0);
            return device;
        }
        return null;
    }

    @Override
    public Device getDevicecc(String carryCode) {
        DeviceExample example = new DeviceExample();
        example.or().andAppCodeEqualTo(carryCode);
        List<Device> list = deviceMapper.selectByExample(example);
        if (list != null && list.size() != 0) {
            Device device = list.get(0);
            return device;
        }
        return null;
    }

    @Override
    public void deleteDevice(int id) {

        deviceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> findDeviceApk(int isUpdated, String deviceId, String companyName, String companyCode, String lineShort, String lineName, int startRow, int pageSize) {
        try {
            List<Map<String, Object>> list = deviceMapper.findDeviceApk(isUpdated, deviceId, companyName, companyCode, lineShort, lineName, startRow, pageSize);
            if (list != null && list.size() != 0) {
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int findDeviceApkCount(int isUpdated, String deviceId, String companyName, String companyCode, String lineShort, String lineName) {
        try {
            int count = deviceMapper.findDeviceApkCount(isUpdated, deviceId, companyName, companyCode, lineShort, lineName);
            if (count != 0) {
                return count;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

}
